//
//  WWLocationManager.m
//  WhatWeather
//
//  Created by byn on 15/12/1.
//  Copyright © 2015年 byn. All rights reserved.
//

#import "WWLocationManager.h"

@implementation WWLocationManager

- (void) updateLocation:(updateSucc)updateSucc{
    
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
    }
    _updateSucc = updateSucc;
    if (![CLLocationManager locationServicesEnabled]) {
            //定位没有打开
        return;
    }
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
        [_locationManager requestWhenInUseAuthorization];
    } else if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse){
        [_locationManager startUpdatingLocation];
        NSLog(@"updateLocation");
    }
}

- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) {
        NSLog(@"didChangeAuthorizationStatus");
        [_locationManager startUpdatingLocation];
    }
}
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    NSLog(@"didUpdateLocations");
    if (!_geocoder) {
        _geocoder = [[CLGeocoder alloc] init];
    }
    
    [_geocoder reverseGeocodeLocation:[locations lastObject] completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            //  NSString *cityName = [[placemarks firstObject].addressDictionary objectForKey:@"City"];
        NSString *cityName = [placemarks firstObject].locality;
        
        _updateSucc(cityName);
    }];
    [_locationManager stopUpdatingLocation];
}
@end














































